//
//  WaterFlowLayout.m
//  HYHoney
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "WaterFlowLayout.h"

@interface WaterFlowLayout ()

@property (nonatomic, assign) NSInteger numberOfItem;
@property (nonatomic, retain) NSMutableArray *columnHeights;  // 保存每一列的高度
@property (nonatomic, retain) NSMutableArray *arrForAttributes;  // 保存item 属性的数组
@property (nonatomic, assign) CGFloat xForItemOrigin;  // item 的x值
@property (nonatomic, assign) CGFloat yForItemOrigin;  // item 的y值
@property (nonatomic, assign) NSInteger shortIndex;

@end


@implementation WaterFlowLayout

- (void)prepareLayout{

    [super prepareLayout];
    
    self.columnHeights = @[@(10),@(10)].mutableCopy;
    self.arrForAttributes = [NSMutableArray array];
    
    // 获取 item 的数量
    self.numberOfItem = [self.collectionView numberOfItemsInSection:0];
    
    for (int i = 0; i < self.numberOfItem; i++) {
        
        [self getItemOriginPoint];
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [self setFrame:indexPath];
    }
}


// 最短的列
- (void)getItemOriginPoint{

    NSInteger shortIndex = 0;
    CGFloat shortHeight = MAXFLOAT;
    
    for (int i = 0; i < 2; i++) {
        
        CGFloat currentHeight = [self.columnHeights[i] floatValue];
        
        if (currentHeight < shortHeight) {
            
            shortHeight = currentHeight;
            shortIndex = i;
        }
    }
    self.shortIndex = shortIndex;
    
    self.xForItemOrigin = 10 + (self.itemSize.width + self.insertItemSpacing) * shortIndex;
    self.yForItemOrigin = shortHeight + self.insertItemSpacing;
}


- (void) setFrame:(NSIndexPath *)indexPath{

    UICollectionViewLayoutAttributes *layoutArr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    CGFloat itemHeight = 0;
    if (_delegate && [_delegate respondsToSelector:@selector(heightForItemIndexPath:)]) {
        
        itemHeight = [_delegate heightForItemIndexPath:indexPath];
    }
    
    layoutArr.frame = CGRectMake(self.xForItemOrigin, self.yForItemOrigin, self.itemSize.width, itemHeight);
    
    [self.arrForAttributes addObject:layoutArr];
    
    self.columnHeights[_shortIndex] = @(self.yForItemOrigin + itemHeight);
}


// 获取最长列的索引
- (NSInteger)p_indexForLongColum{

    NSInteger longIndex = 0;
    CGFloat longHeight = 0;
    
    for (int i = 0; i < 2; i++) {
        
        CGFloat currentHeight = [self.columnHeights[i] floatValue];
        if (currentHeight > longHeight) {
            longHeight = currentHeight;
            longIndex = i;
        }
    }
    return longIndex;
}




// 计算contentView 大小
- (CGSize)collectionViewContentSize{

    NSInteger longIndex = [self p_indexForLongColum];
    
    CGFloat longHeight = [self.columnHeights[longIndex] floatValue];
    CGSize contentSize = self.collectionView.frame.size;
    contentSize.height = longHeight + 10;
    
    return contentSize;
}


// 返回每一个item 的attribute
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{

    return self.arrForAttributes;
}


- (void)dealloc{

    [self.columnHeights release];
    [self.arrForAttributes release];
    [super dealloc];
}


@end
